﻿using AutoMapper;
using B.S.DataBase.SuYuan.Domains.Entites;
using B.S.DataBase.SuYuan.ErrorCode;
using B.S.DataBase.SuYuan.Infrastructure.Interfaces;
using B.S.DataBase.SuYuan.Write.API.Applications.Commands.ShopInfo;
using MediatR;

namespace B.S.DataBase.SuYuan.Write.API.Applications.CommandHandlers.ShopInfo
{
    public class ShopUpdateCommandHandler:IRequestHandler<ShopUpdateCommand,APIResult<object>>
    {
        private readonly IBaseRepository<ShopInfoModel> sRepo;
        private readonly IMapper mapper;

        public ShopUpdateCommandHandler(IBaseRepository<ShopInfoModel>sRepo,IMapper mapper)
        {
            this.sRepo = sRepo;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(ShopUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var getObj = sRepo.GetAll().FirstOrDefault(x => x.ShopId == request.ShopId);
            getObj.UpdateTime = DateTime.Now;
            getObj.UpdateName = request.UpdateName;
            getObj.ShopTypeId = request.ShopTypeId;
            getObj.BatchId = request.BatchId;
            getObj.BaaoZhiQi = request.BaaoZhiQi;
            getObj.Productionnumber = request.Productionnumber;
            getObj.Manufacturers = request.Manufacturers;
            getObj.FengMian = request.FengMian;
            var obj = sRepo.Update(getObj).Result;
            if (obj > 0)
            {
                result.Code = ResultCode.修改成功;
                result.Msg = ResultCode.修改成功.ToString();
                result.Data = obj;
            }
            else
            {
                result.Code = ResultCode.修改失败;
                result.Msg = ResultCode.修改失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
